﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.CarModel;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class CarList : Aliyeye.Car.Net.UI.BaseForm
    {
        CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();
        private bool isDriver = false;
        private string strSearch = string.Empty;
        private int driverRole = 0;
        //DataSet dtDepart = null;
       // DataSet dtRole = null;

        public CarList()
        {
            InitializeComponent();
        }

        public CarList(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

        private void CarList_Load(object sender, EventArgs e)
        {
            GetDriverRole();
            BindComboBoxInfo();
            this.pager1.PageCurrent = 1;
            this.pager1.Bind();
        }

        private void BindComboBoxInfo()
        {
 
            this.txtDrive.SelectedIndex = 0;
        }

        private void GetDriverRole()
        {
            CarSvc.CarServiceSoapClient client = new Aliyeye.Car.Net.UI.CarSvc.CarServiceSoapClient();
            
            string strWhere = string.Empty;
            DataSet dtRole = null;
            dtRole = client.GetList(sopaHeader,strWhere);
            if (dtRole.Tables[0].Rows.Count > 0)
            {
                if (dtRole.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    driverRole = int.Parse(dtRole.Tables[0].Rows[0]["Id"].ToString());
                }
            }
        }

        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = strSearch;
            PageData pageData = new PageData(this, pager1, this.overlay);
            pageData.TableName = "Car";
            pageData.PrimaryKey = "Id";
            pageData.OrderStr = "Id desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = " ID,IsActive,Code,NextMile,Drive,CarType,Color,Owners,OwnerTel,BuyPrice,BuyTime ";

            dgvInfo.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }

        private int pager1_EventPaging(EventPagingArg e)
        {
            return dgvBind();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }

        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            String id = dgvInfo.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            if (strButtonContent.Trim().Equals("修改"))
            {
                AddCarsNew AddCarsForm = new AddCarsNew(isDriver, Convert.ToInt32(id));
                AddCarsForm.lblTitle.Text = "修改车辆";
                AddCarsForm.ShowDialog();
            }

            else if (strButtonContent.Trim().Equals("删除"))
            {
                //"确定删除？"
                DialogConfirm diaSecc = new DialogConfirm("确定删除此车辆信息吗？");
                if (diaSecc.ShowDialog() == DialogResult.OK)
                {
                    //Model.Car model = new Model.Car();
                    //model.Id = Convert.ToInt32(id);
                    //BLL.Car bll = new BLL.Car();
                    //bll.Delete(model.Id);
                    ////txtTrueName.Text = "";
                    ////txtUserName.Text = "";
                    ////cbbDepart.SelectedIndex = 0;
                    ////cbbRole.SelectedIndex = 0;
                    //DialogNotify notify = new DialogNotify("删除车辆信息成功！");
                    //notify.ShowDialog();

                    ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                   proxy => proxy.Delete(sopaHeader, Convert.ToInt32(id)),
                   result =>
                   {
                       if (result.Error == null)
                       {
                           PageCommon.Notify("删除车辆信息成功！");
                           txtCode.Text = "";
                           txtCarOwner.Text = "";
                           txtClor.Text = "";
                           txtDrive.SelectedIndex = 0;
                           overlay.FadeOut();Cursor.Current = Cursors.Default;
                       }
                       else
                       {
                           PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                       }
                   },
                   this
               );
                }

                
            }
            else if (strButtonContent.Trim().Equals("查看"))
            {
                CarsInfoNew userForm = new CarsInfoNew(isDriver, Convert.ToInt32(id));
                userForm.ShowDialog();
            }
            this.pager1.Bind();
        }

        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        //搜索
        private void btnSearch_Click(object sender, EventArgs e)
        {
            strSearch = "";
            if (!string.IsNullOrEmpty(txtDrive.SelectedIndex.ToString()))
            {
                bool Drive = Convert.ToBoolean(txtDrive.SelectedIndex);
                strSearch += " Drive = '" + Drive + "'";
            }
            if (!string.IsNullOrEmpty(txtCode.Text))
            {
                strSearch += " And Code like '%" + txtCode.Text + "%'";
            }

            if (!string.IsNullOrEmpty(txtClor.Text))
            {
                strSearch += " And Color like '%" + txtClor.Text + "%'";
            }

           

            if (!string.IsNullOrEmpty(txtCarOwner.Text))
            {
                strSearch += " And Owners like '%" + txtCarOwner.Text + "%'";
            }
            this.pager1.Bind();
        }

        //全部
        private void btnAll_Click(object sender, EventArgs e)
        {

            strSearch = "";
            this.pager1.Bind();
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "驾驶方式")
            {
                if (e.Value != null)
                {
                    if (Convert.ToBoolean(e.Value))
                    {
                        e.Value = "带驾";
                    }
                    else
                    {
                        e.Value = "自驾";
                    }
                }
            }

            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "状态")
            {
                if (e.Value != null)
                {
                    if (Convert.ToBoolean(e.Value))
                    {
                        e.Value = "营业中";
                    }
                    else
                    {
                        e.Value = "停驶中";
                        e.CellStyle.ForeColor = Color.Red;
                    }
                }
            }
        }

    }
    
}
